//
//  CheckPhoneView.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/10.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "CheckPhoneView.h"
#import "RechargeViewController.h"

@interface CheckPhoneView()
@property (nonatomic, strong)UITextField * phoneTextField;
@property (nonatomic, strong)UITextField * verificationCodeTextField;
@property (nonatomic, strong)UIButton * servePhoneBtn;//客服电话
@end

@implementation CheckPhoneView(networking)
//绑定验证手机号
- (void)bindPhoneNum{
    NSDictionary * dic = @{
                           @"openid":@"88888888",
                           @"mobile":self.phoneTextField.text,
                           @"code":@"8888"
                           };
    [[AFAppDotNetAPIClient sharedClient] POST:@"plan/bindMobile" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"1"]) {
            [self cancel];
            RechargeViewController * rechargeVC = [[RechargeViewController alloc] init];
            [rechargeVC verifyPhoneNum];
        }else{
            [self makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self makeToast:@"操作失败" duration:1 position:CSToastPositionCenter];
    }];
}
@end

@implementation CheckPhoneView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        //背景
        UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT)];
        bgView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:bgView];
        //搭建控件
        [self setUpViews];
    }return self;
}

- (void)setUpViews{
    //白色背景
    UIView * whiteBGView = [[UIView alloc] init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.cornerRadius = 4;
    whiteBGView.layer.masksToBounds = YES;
    [self addSubview:whiteBGView];
    //标题
    UILabel * titleLab = [[UILabel alloc] init];
    titleLab.text = @"验证手机号";
    titleLab.font = DEF_FontSize_14;
    titleLab.textColor = [UIColor blackColor];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.backgroundColor = [UIColor clearColor];
    [self addSubview:titleLab];
    //手机号
    self.phoneTextField = [[UITextField alloc] init];
    self.phoneTextField.placeholder = @"输入手机号";
    self.phoneTextField.font = DEF_FontSize_14;
    self.phoneTextField.backgroundColor = getColor(@"f5f5f5");
    self.phoneTextField.layer.cornerRadius = 4;
    self.phoneTextField.layer.masksToBounds = YES;
    [self addSubview:self.phoneTextField];
    //验证码
    self.verificationCodeTextField = [[UITextField alloc] init];
    self.verificationCodeTextField.placeholder = @"输入验证码";
    self.verificationCodeTextField.font = DEF_FontSize_14;
    self.verificationCodeTextField.backgroundColor = getColor(@"f5f5f5");
    self.verificationCodeTextField.layer.cornerRadius = 4;
    self.verificationCodeTextField.layer.masksToBounds = YES;
    [self addSubview:self.verificationCodeTextField];
    //验证码按钮
    UIButton * getVerificationCodeBtn = [[UIButton alloc] init];
    [getVerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getVerificationCodeBtn setBackgroundColor:[UIColor clearColor]];
    [getVerificationCodeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    getVerificationCodeBtn.titleLabel.font = DEF_FontSize_14;
    [self addSubview:getVerificationCodeBtn];
    //获取不到验证码？
    UILabel * serveLab = [[UILabel alloc] init];
    serveLab.text = @"收不到验证码？请拨打客服电话";
    serveLab.font = DEF_FontSize_10;
    serveLab.textColor = getColor(@"d2d2d2");
    serveLab.textAlignment = NSTextAlignmentLeft;
    serveLab.backgroundColor = [UIColor clearColor];
    [self addSubview:serveLab];
    //电话
    UIButton * servePhoneBtn = [[UIButton alloc] init];
    [servePhoneBtn setTitle:@"400-666-8888" forState:UIControlStateNormal];
    [servePhoneBtn setTitleColor:getColor(@"49cdae") forState:UIControlStateNormal];
    servePhoneBtn.titleLabel.font = DEF_FontSize_10;
    [servePhoneBtn addTarget:self action:@selector(servePhoneBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:servePhoneBtn];
    self.servePhoneBtn = servePhoneBtn;
    //验证按钮
    UIButton * verificationBtn = [[UIButton alloc] init];
    [verificationBtn setTitle:@"验证" forState:UIControlStateNormal];
    verificationBtn.titleLabel.font = DEF_FontSize_14;
    [verificationBtn setBackgroundColor:getColor(@"49cdae")];
    [verificationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    verificationBtn.layer.cornerRadius = 4;
    verificationBtn.layer.masksToBounds = YES;
    [verificationBtn addTarget:self action:@selector(verificationBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [verificationBtn setBackgroundImage:[UIImage imageNamed:@"maself_bg"] forState:UIControlStateNormal];
    [self addSubview:verificationBtn];
    //masorny
    __weak typeof (self)weakSelf = self;
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).mas_offset(29/WIDTH_6_SCALE);
        make.right.equalTo(weakSelf.mas_right).mas_offset(-29/WIDTH_6_SCALE);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.height.mas_offset(256/WIDTH_6_SCALE);
    }];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(whiteBGView);
        make.top.equalTo(whiteBGView.mas_top).mas_offset(20/WIDTH_6_SCALE);
        make.height.mas_offset(18/WIDTH_6_SCALE);
    }];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(whiteBGView.mas_left).mas_offset(17/WIDTH_6_SCALE);
        make.right.equalTo(whiteBGView.mas_right).mas_offset(-17/WIDTH_6_SCALE);
        make.top.equalTo(titleLab.mas_bottom).mas_offset(24/WIDTH_6_SCALE);
        make.height.mas_offset(40/WIDTH_6_SCALE);
    }];
    [self.verificationCodeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.width.height.equalTo(weakSelf.phoneTextField);
        make.top.equalTo(weakSelf.phoneTextField.mas_bottom).mas_offset(10/WIDTH_6_SCALE);
    }];
    [getVerificationCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.verificationCodeTextField.mas_right).mas_offset(-11/WIDTH_6_SCALE);
        make.top.bottom.equalTo(weakSelf.verificationCodeTextField);
    }];
    [serveLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneTextField.mas_left);
        make.top.equalTo(self.verificationCodeTextField.mas_bottom).mas_offset(13/WIDTH_6_SCALE);
        make.height.mas_offset(13/WIDTH_6_SCALE);
    }];
    [servePhoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(serveLab.mas_right).mas_offset(2/WIDTH_6_SCALE);
        make.top.height.equalTo(serveLab);
    }];
    [verificationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.phoneTextField);
        make.top.equalTo(serveLab.mas_bottom).mas_offset(23/WIDTH_6_SCALE);
        make.height.mas_offset(40/WIDTH_6_SCALE);
    }];
}
#pragma mark - action
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)cancel{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)verificationBtnClick{
    NSLog(@"验证");
//    [self cancel];
//    if (self.bindPhoneNumBlock) {
//        self.bindPhoneNumBlock();
//    }
    [self bindPhoneNum];
}
- (void)servePhoneBtnClick{
    NSMutableString *str = [[NSMutableString alloc]initWithFormat:@"tel:%@",self.servePhoneBtn.titleLabel.text];
    UIWebView *callWebview = [[UIWebView alloc]init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self addSubview:callWebview];
}
@end
